package cn.kgc.crowdfundingadmin2.controller;



import cn.kgc.crowdfundingadmin2.common.ResultEntity;
import cn.kgc.crowdfundingadmin2.pojo.Advertisement;
import cn.kgc.crowdfundingadmin2.service.AdvertisementService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create
 */
@Controller
public class AdvertisementController {
    @Resource
    AdvertisementService advertisementService;

    @ResponseBody
    @RequestMapping("/advertisement/search/by/keyword")
    public ResultEntity<PageInfo<Advertisement>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {

        // 1.查询得到PageInfo对象
        PageInfo<Advertisement> pageInfo = advertisementService.queryForKeywordWithPage(pageNum, pageSize, keyword);

        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }


    //回显删除信息
    @ResponseBody
    @RequestMapping("/advertisement/get/list/by/id/list")
    public ResultEntity<List<Advertisement>> getRoleListByIdList(@RequestBody List<Integer> advertisementIdList) {

        List<Advertisement> advertisementList = advertisementService.getAdvertisementListByIdList(advertisementIdList);

        return ResultEntity.successWithoutData(advertisementList);
    }

    //删除选定信息
    @ResponseBody
    @RequestMapping("/advertisement/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> advertisementIdList) {

        advertisementService.batchRemove(advertisementIdList);

        return ResultEntity.successWithoutData();
    }

    //新增
    @ResponseBody
    @RequestMapping("/advertisement/save/advertisement")
    public ResultEntity<String> saveAdvertisement(@RequestParam("advertisementName") String advertisementName,@RequestParam("advertisementStatus")Byte  advertisementStatus) {

        advertisementService.saveAdvertisement(advertisementName,advertisementStatus);

        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/advertisement/update/advertisement")
    public ResultEntity<String> updateAdvertisement(Advertisement advertisement) {

        advertisementService.updateAdvertisement(advertisement);

        return ResultEntity.successWithoutData();
    }
}
